﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LAN_Party_Manager
{
    public partial class frmGame : Form
    {
        public frmGame()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int x = 1;
            foreach (Player y in Player.players)
            {
                if (y.isActive())
                {
                    if (x % 2 == 0)
                    {
                        y.setTeam(2);
                        lstTeam2.Items.Add(y.getName());
                    }
                    else
                    {
                        y.setTeam(1);
                        lstTeam1.Items.Add(y.getName());
                    }
                    x++;
                    foreach (object o in lstActive.Items)
                    {
                        if (lstActive.GetItemText(o) == y.getName())
                        {
                            lstActive.Items.Remove(o);
                            break;
                        }
                    }
                }

            }
        }

        private void frmGame_Load(object sender, EventArgs e)
        {
            Player.players.Sort((Player x, Player y) => x.getElo() - y.getElo());
            lstActive.Items.Clear();
            if (Player.players.Count > 0)
                foreach (Player p in Player.players)
                    if (p.isActive())
                        lstActive.Items.Add(p.getName());
        }

        private void btnToT1_Click(object sender, EventArgs e)
        {
            btnGenerate.Enabled = false;
            foreach (Player t in Player.players)
            {
                if (t.getName() == lstActive.GetItemText(lstActive.SelectedItem))
                {
                    t.setTeam(1) ;
                    lstTeam1.Items.Add(t.getName());
                    break;
                }
            }
            lstActive.Items.Remove(lstActive.SelectedItem);
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            if ((lstTeam1.Items.Count - lstTeam2.Items.Count > 1) || (lstTeam1.Items.Count - lstTeam2.Items.Count < -1))
            {
                MessageBox.Show("Teams aren't close to being even!");
                return;
            }
            if (lstActive.Items.Count > 0)
            {
                MessageBox.Show("Not all active players have been assigned a team.");
                return;
            }
            btnGenerate.Enabled = true;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnFromT1_Click(object sender, EventArgs e)
        {
            foreach (Player t in Player.players)
            {
                if (t.getName() == lstTeam1.GetItemText(lstTeam1.SelectedItem))
                {
                    t.setTeam(0);
                    lstActive.Items.Add(t.getName());
                    break;
                }
            }
            lstTeam1.Items.Remove(lstTeam1.SelectedItem);
        }

        private void btnToT2_Click(object sender, EventArgs e)
        {
            btnGenerate.Enabled = false;
            foreach (Player t in Player.players)
            {
                if (t.getName() == lstActive.GetItemText(lstActive.SelectedItem))
                {
                    t.setTeam(2);
                    lstTeam2.Items.Add(t.getName());
                    break;
                }
            }
            lstActive.Items.Remove(lstActive.SelectedItem);
        }

        private void btnFromT2_Click(object sender, EventArgs e)
        {
            foreach (Player t in Player.players)
            {
                if (t.getName() == lstTeam2.GetItemText(lstTeam2.SelectedItem))
                {
                    t.setTeam(0);
                    lstActive.Items.Add(t.getName());
                    break;
                }
            }
            lstTeam2.Items.Remove(lstTeam2.SelectedItem);
        }
    }
}
